﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace System
{
    public static class StringHelpers
    {
        public static bool HasValue(this string item)
        {
            return !string.IsNullOrEmpty(item);
        }

        public static bool HasNoValue(this string item)
        {
            return string.IsNullOrEmpty(item);
        }

        public static bool IsNotPostcode(this string item)
        {
            return !IsPostcode(item);
        }

        public static bool IsPostcode(this string item)
        {
            bool isValid = false;

            // Ignore if empty or null
            if (string.IsNullOrEmpty(item))
            {
                isValid = false;
            }
            else
            {
                string regEx = "^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$";

                // Do a regex match to ensure an email address
                isValid = Regex.IsMatch(item, regEx);
            }

            return isValid;
        }

        public static bool IsNotEmail(this string item)
        {
            return !IsEmail(item);
        }

        public static bool IsEmail(this string item)
        {
            bool isEmail = false;

            // Ignore if empty or null
            if (string.IsNullOrEmpty(item))
            {
                isEmail = false;
            }
            else
            {
                string regEx = @"[\-!#$%&'*+/=?^_`{|}~A-Za-z0-9]+(\.[\-!#$%&'*+/=?^_`{|}~A-Za-z0-9]+)*\.{0,1}@([\-_A-Za-z0-9]+\.)*[\-_A-Za-z0-9]+\.[\-_A-Za-z0-9]{2,4}";

                // Do a regex match to ensure an email address
                isEmail = Regex.IsMatch(item, regEx);
                if(isEmail)
                {
                    //List of lower case banned addresses
                    string[] bannedAddresses = new[]
                    {
                        "test@test.com"
                    };
                    if (bannedAddresses.Contains(item.ToLower()))
                    {
                        isEmail = false;
                    }
                }
            }
            return isEmail;
        }

        public static string StringConcat(this IEnumerable<string> source, string spacer)
        {
            System.Text.StringBuilder tmp = new System.Text.StringBuilder();
            string tmpSpacer = "";
            foreach (var item in source)
            {
                tmp.AppendFormat("{0}{1}", tmpSpacer, item);
                tmpSpacer = spacer;
            }
            return tmp.ToString();

        }

        public static string StringConcat(this IEnumerable<string> source, Func<string, string> selector, string spacer)
        {
            return StringConcat(source.Select(selector), spacer);
        }

        public static string StringConcat<TSource>(this IEnumerable<TSource> source, Func<TSource, string> selector, string spacer)
        {
            return StringConcat(source.Select(selector), spacer);
        }

        public static string Append(this string source, string value)
        {   
            return string.Concat(source, value);
        }

        public static string Append(this string source, object value)
        {
            return string.Concat(source, value);
        }

        public static string AppendFormat(this string source, string value, params object[] args)
        {
            return source.Append(string.Format(value, args));
        }

        public static string ToCamelCase(this string source)
        {
            if(source != null)
            {
                return string.Concat(source.Substring(0,1).ToLower(), source.Substring(1));
            }
            return null;
        }


    }
}
